package top.carrotguo.weibo.model;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import top.carrotguo.weibo.bean.HomeTimeLine;
import top.carrotguo.weibo.contract.PersonHomeContract;
import top.carrotguo.weibo.retrofit.RetrofitUtil;
import top.carrotguo.weibo.retrofit.api.StatusService;
import top.carrotguo.weibo.retrofit.callback.BaseCallback;
import top.carrotguo.weibo.retrofit.callback.ErrorInfo;
import top.carrotguo.weibo.utils.MyConstans;

public class PersonHomeModel implements PersonHomeContract.Model {
    @Override
    public void getBlogList(String accessToken, String uid, BaseCallback<HomeTimeLine, ErrorInfo> callback) {
        StatusService service = RetrofitUtil.getInstance().create(StatusService.class);
        Call<HomeTimeLine> call = service.getUserBlog(accessToken,uid);
        call.enqueue(new Callback<HomeTimeLine>() {
            @Override
            public void onResponse(Call<HomeTimeLine> call, Response<HomeTimeLine> response) {
                if (response != null) {
                    HomeTimeLine info = response.body();
                    if (info.getError_code() == null) {
                        callback.onSuccessed(info);
                    } else {
                        callback.onError(new ErrorInfo(Integer.parseInt(info.getError_code()),info.getError()));
                    }
                } else {
                    callback.onError(new ErrorInfo(MyConstans.REQUEST_LIMIT,"访问受限"));
                }
            }

            @Override
            public void onFailure(Call<HomeTimeLine> call, Throwable t) {
                callback.onError(new ErrorInfo(MyConstans.CALL_FAILURE,"接口有误"));
            }
        });
    }
}
